<?php

namespace app\club\controller;

use app\common\controller\Clubend;

use think\Controller;
use think\Request;
use think\Db;
use app\club\model\CUser;
use app\club\model\CUserLog;
use app\club\model\CUserApply;
use think\Session;
use fast\Http;
use think\Config;

/**
 * 俱乐部玩家管理
 *
 * @icon fa fa-circle-o
 */
class Userapply extends Clubend
{
    
    /**
     * CUserLog模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('CUserApply');
        $this->assign('title','成员申请');
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
     * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    
    /**
     * 查看
     */
    public function index()
    {
        //当前俱乐部信息
        $clubid = (int)$this->request->param('clubid');
        if(empty($clubid)){
            $clubid = Session::get('clubid');
        }else{
            Session::set("clubid", $clubid);
        }
        if($clubid <= 0){
            return $this->redirect('/club/club/index');
        }
    
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
            ->where($where)
            ->where(['clubid'=>$clubid,'status'=>0,'type'=>array('in','1,2')])
            ->order('createtime desc')
            ->count();

            $list = $this->model
            ->where($where)
            ->where(['clubid'=>$clubid,'status'=>0,'type'=>array('in','1,2')])
            ->order('createtime desc')
            ->limit($offset, $limit)
            ->select();
            
            //处理数据
            $uid_arr = $map_user = $user_arr = [];
            foreach ($list as $val){
                $uid_arr[] = $val['uid'];
            }
            $map_user['uid'] = array('in',$uid_arr);
            $user_list = $this->selectTbale($this->GameDBConfig,'user',$map_user,'uid,nickname');//用户列表
            foreach ($user_list as $val){
                $user_arr[$val['uid']] = $val['nickname'];
            }
            foreach ($list as $key=>$val){
                $list[$key]['nickname'] = isset($user_arr[$val['uid']])?$user_arr[$val['uid']]:'-';
                $list[$key]['typetext'] = $val->getData('type');
            }
            
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }
    
    /**
     * 同意申请
     * @author violet
     * @since 2017-11-23
     */
    public function agree($clubid = 0,$uid = 0,$type=0){
        //基础判断
        if (!$this->request->isPost()){
            $this->error('请正确操作');
        }
        if(empty($clubid) || empty($uid) || empty($type)){
            $this->error('请选择要执行的信息');
        }
        $club_user = Db::table('fa_c_user')->where(['clubid'=>$clubid,'uid'=>$uid])->find();
        //加入 并 同意
        if( $type == 1){
            
            $usercount = Db::table('fa_c_user')->where(['clubid'=>$clubid])->count();
            if($usercount >= 500){
                $this->error('俱乐部人员已满');
            }
            if(!empty($club_user)){
                $this->error('已经是该俱乐部成员');
            }
            
            //游戏服务器
            $sreq = [
                'id' => (int)$clubid,
                'uid' => (int)$uid,
                'apply' => true,
            ];
             
            //发送给游戏服务器
            $res = Http::get(Config::get('GameDB.api_url').'responseClubJoin',$sreq);
            if($res != 'ok')
            {
                $this->error($res);
            }
        }
        //退出 并 同意
        if($type == 2){
            
            if(empty($club_user)){
                $this->error('不是该俱乐部成员');
            }
            //游戏服务器
            $sreq = [
                'id' => (int)$clubid,
                'uid' => (int)$uid,
                'apply' => true,
            ];
             
            //发送给游戏服务器
            $res = Http::get(Config::get('GameDB.api_url').'responseClubQuit',$sreq);
            if($res != 'ok')
            {
                $this->error($res);
            }
        }
        $this->success('操作成功');
    }
    
    /**
     * 拒绝申请
     * @param int $ids
     * @author violet
     * @since 2017-11-23
     */
    public function refuse($clubid = 0,$uid = 0,$type=0){
        if (!$this->request->isPost()){
            $this->error('请正确操作');
        }
        if(empty($clubid) || empty($uid) || empty($type)){
            $this->error('请选择要执行的信息');
        }
        //加入 拒绝
        if( $type == 1){
            //游戏服务器
            $sreq = [
                'id' => (int)$clubid,
                'uid' => (int)$uid,
                'apply' => false,
            ];
             
            //发送给游戏服务器
            $res = Http::get(Config::get('GameDB.api_url').'responseClubJoin',$sreq);
            if($res != 'ok')
            {
                $this->error($res);
            }
        }
        //退出 拒绝
        if($type == 2){
            //游戏服务器
            $sreq = [
                'id' => (int)$clubid,
                'uid' => (int)$uid,
                'apply' => false,
            ];
             
            //发送给游戏服务器
            $res = Http::get(Config::get('GameDB.api_url').'responseClubQuit',$sreq);
            if($res != 'ok')
            {
                $this->error($res);
            }
        }
        
        $this->success('操作成功');
    }

}
